package com.wbdgj.ui.branch;

import android.content.Context;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.FootprintBranchAdapter;
import com.wbdgj.adapter.NetfreeRoamingHistoryAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

public class NetfeeRoamingHistoryActivity extends BaseActivity {

    private Context context = this;

    @BindView(R.id.demo_listview)
    PullListView demo_listview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    com.wbdgj.adapter.NetfreeRoamingHistoryAdapter netfreeRoamingHistoryAdapter;

    private int page = 1;
    private int total;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_netfree_roaming_history;
    }

    @Override
    protected void initView() {
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        loglist();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if ((page*10)<total){
                    page ++;
                    loglist();
                }else{
                    ToastUtils.toastShort("已无更多");
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
        loglist();
    }

    private void loglist() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().loglist(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), page+"","10");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    List<LinkedTreeMap> linkedTreeMaps = (List<LinkedTreeMap>) linkedTreeMap.get("data");
                    total = Integer.parseInt(decimalFormat.format(linkedTreeMap.get("total")));
                    if (page == 1){
                        netfreeRoamingHistoryAdapter = new NetfreeRoamingHistoryAdapter(context, linkedTreeMaps );
                        demo_listview.setAdapter(netfreeRoamingHistoryAdapter);
                    }else{
                        netfreeRoamingHistoryAdapter.loadMoreListView(linkedTreeMaps);
                        netfreeRoamingHistoryAdapter.notifyDataSetChanged();
                    }

                }
            }
        });
    }
}
